package com.run.runbbs.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.run.runbbs.common.dto.BindingFamilyDto;
import com.run.runbbs.common.dto.UserFamilyDto;
import com.run.runbbs.common.lang.Result;
import com.run.runbbs.entity.User;
import com.run.runbbs.entity.UserFamily;
import com.run.runbbs.entity.UserInfo;
import com.run.runbbs.service.UserFamilyService;
import com.run.runbbs.service.UserInfoService;
import com.run.runbbs.service.impl.UserService;
import com.run.runbbs.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/userFamily")
public class UserFamilyController {
    @Autowired
    private UserFamilyService userFamilyService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private JwtUtils jwtUtils;

    @GetMapping("/getFamily")
    public Result get(HttpServletRequest request){
        String jwt=request.getHeader("Authorization");
        int userId=Integer.parseInt(jwtUtils.getClaimByToken(jwt).getSubject());
       List<UserFamily> list =userFamilyService.list(new QueryWrapper<UserFamily>().eq("user_id",userId));
        List<UserFamilyDto> result=new ArrayList<>();
        for(UserFamily userFamily : list){
            UserFamilyDto userFamilyDto=new UserFamilyDto();
            userFamilyDto.setUserId(userFamily.getUserId());
            userFamilyDto.setMemberId(userFamily.getMemberId());
            userFamilyDto.setMemberType(userFamily.getMemberType());
            UserInfo userInfo=userInfoService.getOne(new QueryWrapper<UserInfo>().eq("user_id",userFamily.getMemberId()));
            userFamilyDto.setMemberName(userInfo.getRealname());
            result.add(userFamilyDto);
        }
       return Result.success(result);
    }
    @PostMapping("bindingFamily")
    public Result bindingFamily(@RequestBody BindingFamilyDto bindingFamilyDto, HttpServletRequest request){
        String jwt=request.getHeader("Authorization");
        int userId=Integer.parseInt(jwtUtils.getClaimByToken(jwt).getSubject());
        User mine=userService.findUserById(userId);
        User member=userService.findUserByEmail(bindingFamilyDto.getEmail());
        //邮箱未注册，不能绑定
        if(member==null){
            return Result.fail("该邮箱暂未绑定用户，请前往注册！");
        }
        if(mine.getEmail().equals(member.getEmail())){
            return Result.fail("不能绑定自己！");
        }
        //已经绑定
        int count=userFamilyService.count(new QueryWrapper<UserFamily>().eq("user_id",userId).eq("member_id",member.getId()));
        if (count>0){
            return Result.fail("关系已绑定，请勿重复绑定！");
        }

        //插入表中,相互绑定
        UserFamily userFamily=new UserFamily();
        userFamily.setUserId(userId);
        userFamily.setMemberId(member.getId());
        userFamily.setMemberType(bindingFamilyDto.getType());
        //相互绑定
        UserFamily memberFamily=new UserFamily();
        memberFamily.setUserId(member.getId());
        memberFamily.setMemberId(userId);
        memberFamily.setMemberType(getRelation(bindingFamilyDto.getType()));
        userFamilyService.save(userFamily);
        userFamilyService.save(memberFamily);
        return Result.success("家人绑定成功");
    }

    //返回反向关系
    public String getRelation(String type){
        String relation;
        switch(type)
        {
            case "父亲" :
                relation="儿子";
                break;
            case "母亲" :
                relation="儿子";
                break;
            case "儿子" :
                relation="父亲";
                break;
            case "女儿" :
                relation="父亲";
                break;
            case "爷爷" :
                relation="孙子";
                break;
            case "奶奶" :
                relation="孙子";
                break;
            case "其他" :
                relation="其他";
                break;
            default :
                relation="其他";
        }
        return relation;
    }


    @GetMapping("/getFamilys")
    public Result gets(HttpServletRequest request){
        String jwt=request.getHeader("Authorization");
        int userId=Integer.parseInt(jwtUtils.getClaimByToken(jwt).getSubject());
        List<UserFamily> list=userFamilyService.list(new QueryWrapper<UserFamily>());
        return Result.success(list);
    }
}
